﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DeviceManager.DB.Model;
using DeviceManager.DB;

namespace DeviceManager.ContentPage.SysMng
{
    /// <summary>
    /// EditDeviceInfo.xaml 的交互逻辑
    /// </summary>
    public partial class EditDeviceInfo : Window
    {
        private DeviceNode nowEditDeviceNode = null;

        public EditDeviceInfo()
        {
            InitializeComponent();
        }

        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                this.DragMove();
            }
            catch { }
        }

        private void blt_savedeviceInfo_Click(object sender, RoutedEventArgs e)
        {
            if (tb_deviceCode.Text == "")
            {
                MessageBox.Show("您必须输入设备的编码");
                return;
            }
            if(tb_deviceName.Text == "")
            {
                MessageBox.Show("您必须输入设备名称");
                return;
            }
            float maxVoltage = 0;
            if(!float.TryParse(tb_devicemaxvoltage.Text,out maxVoltage))
            {
                MessageBox.Show("您输入的最大电压值有误，请检查");
                return;
            }
            float minVoltage = 0;
            if(!float.TryParse(tb_deviceminvoltage.Text,out minVoltage))
            {
                MessageBox.Show("您输入的最低电压有误，请检查");
                return;
            }
            float maxcurrent = 0;
            if(!float.TryParse(tb_devicemaxcurrent.Text,out maxcurrent))
            {
                MessageBox.Show("您输入的最大电流值有误，请检查");
                return;
            }
            float minCurrent = 0;
            if(!float.TryParse(tb_devicemincurrent.Text,out minCurrent))
            {
                MessageBox.Show("您输入的最小电流值有误，请检查");
                return;
            }
            float maxtemperature = 0;
            if(!float.TryParse(tb_devicemaxtemperature.Text,out maxtemperature))
            {
                MessageBox.Show("您输入的最大温度有误，请检查");
                return;
            }
            float mintemperature = 0;
            if(!float.TryParse(tb_devicemintemperature.Text,out mintemperature))
            {
                MessageBox.Show("您输入的最低温度值有误，请检查");
                return;
            }
            float maxhumidity = 0;
            if(!float.TryParse(tb_devicemaxhumidity.Text,out maxhumidity))
            {
                MessageBox.Show("您输入的最大湿度值有误，请检查");
                return;
            }
            float minhumidity = 0;
            if(!float.TryParse(tb_deviceminhumidity.Text,out minhumidity))
            {
                MessageBox.Show("您输入的最小湿度值有误，请检查");
                return;
            }
            float maxuptime = 0;
            if(!float.TryParse(tb_devicemaxuptime.Text,out maxuptime))
            {
                MessageBox.Show("您输入的最大数据更新间隔时间有误，请检查");
                return;
            }
            if (!(tb_devicedep.Tag is DepmentInfo))
            {
                MessageBox.Show("您未选择设备所属部门信息，请检查");
                return;
            }
            if (!(tb_deviceType.Tag is DeviceTypeInfo))
            {
                MessageBox.Show("您未选择设备所属类型，请检查");
                return;
            }
            DeviceNode deviceNode = new DeviceNode();
            deviceNode.Id = (nowEditDeviceNode == null) ? 0 : nowEditDeviceNode.Id;
            deviceNode.DeviceName = tb_deviceName.Text;
            deviceNode.DeviceCode = tb_deviceCode.Text;
            deviceNode.DeviceDes = tb_deviceDes.Text;
            deviceNode.DeviceInstall = tb_deviceInstall.Text;
            deviceNode.DeviceMaxVoltage = maxVoltage;
            deviceNode.DeviceMinVoltage = minVoltage;
            deviceNode.DeviceMaxElectricCurrent = maxcurrent;
            deviceNode.DeviceMinElectricCurrent = minCurrent;
            deviceNode.DeviceMaxTemperature = maxtemperature;
            deviceNode.DeviceMinTemperature = mintemperature;
            deviceNode.DeviceMaxHumidity = maxhumidity;
            deviceNode.DeviceMinHumidity = minhumidity;
            deviceNode.DeviceIsMaintain = 0;
            deviceNode.DeviceIsOnLine = false;
            deviceNode.DeviceUpDateTime = maxuptime;
            deviceNode.installDate = DateTime.Now;
            deviceNode.DeviceDepID = (tb_devicedep.Tag as DepmentInfo).Id;
            deviceNode.DeviceType = (tb_deviceType.Tag as DeviceTypeInfo).Id;
            if (ManagerDBCtrl.GetMng().SaveDeviceNode(deviceNode))
            {
                this.DialogResult = true;
            }
            else
            {
                MessageBox.Show("保存设备数据失败");
            }
        }

        private void blt_canneldeviceInfo_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }


        private void tb_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            Regex re = new Regex("[^0-9.-]+");
            e.Handled = re.IsMatch(e.Text);
        }

        internal void SetData(DeviceNode deviceNode)
        {
            nowEditDeviceNode = deviceNode;
            tb_deviceCode.Text = deviceNode.DeviceCode;
            tb_deviceCode.IsReadOnly = true;
            tb_deviceName.Text = deviceNode.DeviceName;
            tb_deviceDes.Text = deviceNode.DeviceDes;
            tb_deviceInstall.Text = deviceNode.DeviceInstall;
            tb_devicemaxvoltage.Text = deviceNode.DeviceMaxVoltage.ToString();
            tb_deviceminvoltage.Text = deviceNode.DeviceMinVoltage.ToString();
            tb_devicemaxcurrent.Text = deviceNode.DeviceMaxElectricCurrent.ToString();
            tb_devicemincurrent.Text = deviceNode.DeviceMinElectricCurrent.ToString();
            tb_devicemaxtemperature.Text = deviceNode.DeviceMaxTemperature.ToString();
            tb_devicemintemperature.Text = deviceNode.DeviceMinTemperature.ToString();
            tb_devicemaxhumidity.Text = deviceNode.DeviceMaxHumidity.ToString();
            tb_deviceminhumidity.Text = deviceNode.DeviceMinHumidity.ToString();
            tb_devicemaxuptime.Text = deviceNode.DeviceUpDateTime.ToString();

            DeviceTypeInfo type = ManagerDBCtrl.GetMng().GetDeviceTypeByID(deviceNode.DeviceType);
            if (type != null)
            {
                this.tb_deviceType.Text = type.Name;
                this.tb_deviceType.ToolTip = type.Des;
                this.tb_deviceType.Tag = type;
            }
            DepmentInfo dep = ManagerDBCtrl.GetMng().GetDepmentInfoByID(deviceNode.DeviceDepID);
            if (dep != null)
            {
                this.tb_devicedep.Text = dep.Name;
                this.tb_devicedep.ToolTip = dep.Des;
                this.tb_devicedep.Tag = dep;
            }

        }

        private void tb_selectdeviceType_Click(object sender, RoutedEventArgs e)
        {
            WindowDeviceTypeSelected wind = new WindowDeviceTypeSelected();
            if (wind.ShowDialog() == true)
            {
                this.tb_deviceType.Text = wind.NowSelectedInfo.Name;
                this.tb_deviceType.ToolTip = wind.NowSelectedInfo.Des;
                this.tb_deviceType.Tag = wind.NowSelectedInfo;
            }
        }

        private void tb_selectdep_Click(object sender, RoutedEventArgs e)
        {
            WindowDepSelected wind = new WindowDepSelected();
            if (wind.ShowDialog() == true)
            {
                this.tb_devicedep.Text = wind.NowSelectedInfo.Name;
                this.tb_devicedep.ToolTip = wind.NowSelectedInfo.Des;
                this.tb_devicedep.Tag = wind.NowSelectedInfo;
            }
        }
    }
}
